/**
 * Created by Administrator on 2017/3/1 0001.
 */
var ratio=window.devicePixelRatio;
var pixiStageW=740;
var pixiStageH=1136;
var renderer=PIXI.autoDetectRenderer(pixiStageW,pixiStageH,{antialias:true,transparent:true,resolution:1});
var stage=new PIXI.Stage(0xFF0000);
var resource;
var pixiStage=stage;

var div_pixi=document.getElementById('div_2d');
div_pixi.appendChild(renderer.view);

var preload=Preload.create();
preload.init(stage,pixiStageW,pixiStageH);

var pageVer=new PIXI.DisplayObjectContainer();

var sp_verbg =new PIXI.Graphics();
sp_verbg.beginFill(0x0,1);
sp_verbg.drawRect(0,0,740,1136);
sp_verbg.endFill();
pageVer.addChild(sp_verbg);

var game;
var sp_help =new PIXI.Sprite.fromImage('assets/images/verHelp.png');
sp_help.anchor.set(0.5);
sp_help.x=370;
sp_help.y=526;
sp_help.rotation=90*Math.PI/180;
pageVer.addChild(sp_help);

TweenMax.to(sp_help,2,{rotation:0*Math.PI/180,repeatDelay:0.5,repeat:-1,yoyo:true,ease:Back.easeInOut});


var sp_vert0 =new PIXI.Sprite.fromImage('assets/images/ver_t0.png');
sp_vert0.anchor.set(0.5);
sp_vert0.scale.x=sp_vert0.scale.y=0.8
sp_vert0.x=370;
sp_vert0.y=685;
pageVer.addChild(sp_vert0);

// document.addEventListener("fullscreenchange", function(e) {
//     console.log("fullscreenchange", e);
//     alert('1');
// });
// document.addEventListener("mozfullscreenchange", function(e) {
//     console.log("mozfullscreenchange ", e);
//     alert('2');
// });
// document.addEventListener("webkitfullscreenchange", function(e) {
//     console.log("webkitfullscreenchange", e);
//     alert('3');
// });
// document.addEventListener("msfullscreenchange", function(e) {
//     console.log("msfullscreenchange", e);
//     alert('4');
// });
document.addEventListener('Main_LoadComplete',gameStart);
function gameStart(e) {
    resource=e.detail.resources;
    game=Game.create();
    stage.addChildAt(game.ui,0);
}

var firstScale={x:0,y:0};
resizeHandler();
window.onresize = resizeHandler;
function resizeHandler() {
    setTimeout(function () {
        renderer.resize(window.innerWidth, window.innerHeight);
        mode='noBorder';
        switch (mode) {
            case 'exactFit':
                stage.scale.x = window.innerWidth / pixiStageW;
                stage.scale.y = window.innerHeight / pixiStageH;
                break;
            case 'noBorder':
                stage.scale.x = (window.innerHeight / pixiStageH < window.innerWidth / pixiStageW) ? (window.innerWidth / pixiStageW) : (window.innerHeight / pixiStageH);
                stage.scale.y = stage.scale.x;
                break;
            case 'noScale':
                stage.scale.x = 1;
                stage.scale.y = 1;
                break;
            case 'showAll':
                stage.scale.x = (window.innerHeight / pixiStageH < window.innerWidth / pixiStageW) ? (window.innerHeight / pixiStageH) : (window.innerWidth / pixiStageW);
                stage.scale.y = stage.scale.x;
                break;
        }
        stage.x = (window.innerWidth - pixiStageW * stage.scale.x) * .5;
        stage.y = (window.innerHeight - pixiStageH * stage.scale.y) * .5;
        if(firstScale.x==0)
        {
            firstScale.x=stage.scale.x;
            firstScale.y=stage.scale.y;
        }

        if(window.innerWidth>window.innerHeight)
        {
            if(pageVer&&!pageVer.parent)
            {
                document.getElementById('div_video1').style.display='none';
                document.getElementById('div_video2').style.display='none';
                stage.addChild(pageVer);
                if(game&&game.ui.parent)
                {
                    game.ui.removeChild(game.ui);
                }

            }
        }
        else
        {
            if(pageVer&&pageVer.parent)
            {
                document.getElementById('div_video1').style.display='block';
                document.getElementById('div_video2').style.display='block';
                stage.removeChild(pageVer);
                stage.addChild(game.ui);
                // stage.scale.x=2;
                // stage.scale.y=2
                // stage.x = (window.innerWidth - pixiStageW * stage.scale.x) * .5;
                // stage.y = (window.innerHeight - pixiStageH * stage.scale.y) * .5;
            }
        }
    },150);


};

var globalID;
globalID=requestAnimationFrame(animate);
function animate() {
    requestAnimationFrame(animate);
    renderer.render(stage);
}